package gui.advertiser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import application.ArenaManager;
import domain.advertisement.Ad;

public class RemoveAdvertisementGUI extends JPanel {

	private JScrollPane scrollPane;
	private JPanel advertisementsPanel;
	
	public RemoveAdvertisementGUI() {
		setLayout(new BorderLayout(0, 0));

		advertisementsPanel = new JPanel(new GridLayout(0, 1, 0, 0));
		
		scrollPane = new JScrollPane();
		scrollPane.setViewportView(loadImage());
		add(scrollPane, BorderLayout.CENTER);

		JLabel lblAdvertiserId = new JLabel("Advertiser ID!");
		scrollPane.setColumnHeaderView(lblAdvertiserId);

		JLabel lblAdvertisements = new JLabel("Remove Ad");
		add(lblAdvertisements, BorderLayout.NORTH);
	}
	
	private JPanel loadImage() {
		for (int i = 0; i < ArenaManager.getInstance().getAds().size(); i++) {
			Ad ad = (Ad) ArenaManager.getInstance().getAds().get(i);

			JButton btnImage = new JButton(ad.getImage());
			btnImage.setBorderPainted(false);
			btnImage.setBackground(Color.white);
			btnImage.setName("" + i);
			btnImage.addActionListener(new ButtonListener());

			advertisementsPanel.add(btnImage);
		}

		return advertisementsPanel;
	}

	class ButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			JButton jbtn = (JButton) e.getSource();
			int index = Integer.parseInt(jbtn.getName());
			ArenaManager.getInstance().removeAd(index);
			//System.out.println("Button ID: " + index);
			
			advertisementsPanel.remove((JButton)e.getSource());
			advertisementsPanel.revalidate();
			advertisementsPanel.updateUI();
			advertisementsPanel.repaint();
		}

	}

}
